package demo1;
import java.lang.Comparable;

public class StudentComparable implements Comparable{
    public String name;
    public int age;

    public StudentComparable(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int compareTo(Object obj) {
        if (obj == this) {
            return 0;
        } else if (obj instanceof StudentComparable) {
            StudentComparable s = (StudentComparable) obj;
            return this.age - s.age;
        }
        throw new RuntimeException("类型不匹配");
    }
}
